package sort;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dx
 * @version 1.0
 * @date 2022/3/22 21:07
 * @description:  给一个整数数组，如果任一值在数组中出现至少两次，返回true；如果数组中每个元素互不相同，返回false
 */
public class Repeat_array {

    public static void main(String args[]){
        int test [] = {1,2,3,4};
        int test2 [] = {1,2,2,3,4};
        boolean t1 = Repeat(test);
        boolean t2 = Repeat(test2);
        System.out.println(t1);
        System.out.println(t2);
    }
//把数组元素：出现次数  保存在HahsMap中
    private static Map<Integer, Integer> Jilu(int nums[]) {
        Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
        for (int num : nums) {
            if (!counts.containsKey(num)) {
                counts.put(num, 1);
            } else {
                counts.put(num, counts.get(num) + 1);
            }
        }
        return counts;
    }
    //判断HashMap中的元素次数是否有大于等于2的，是则返回true 否则返回false
    public static boolean Repeat(int nums[]){

        Map<Integer,Integer> counts = Jilu(nums);
        Map.Entry<Integer,Integer> x = null;
        for(Map.Entry<Integer,Integer> entry : counts.entrySet()){
            if(entry.getValue()>=2){
                return true;
            }
        }
        return false;
    }
}
